package cn.edu.xcu.ckl;

import android.content.DialogInterface;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.lzy.okgo.OkGo;

import cn.edu.xcu.ckl.adapter.OrderListAdapter;
import cn.edu.xcu.ckl.api.ApiConstants;
import cn.edu.xcu.ckl.base.BaseActivity;
import cn.edu.xcu.ckl.entity.OrderInfo;
import cn.edu.xcu.ckl.entity.OrderListInfo;
import cn.edu.xcu.ckl.entity.UserInfo;
import cn.edu.xcu.ckl.http.HttpStringCallback;
import cn.edu.xcu.ckl.utils.GsonUtils;

public class OrderListActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private OrderListAdapter mOrderListAdapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_order_list;
    }

    @Override
    protected void initView() {
        //初始化控件
        recyclerView = findViewById(R.id.recyclerView);
    }

    @Override
    protected void setListener() {
        //初始化mOrderListAdapter
        mOrderListAdapter = new OrderListAdapter(mBaseActivity);

        //设置OrderListAdapter
        recyclerView.setAdapter(mOrderListAdapter);



        //recyclerView点击事件
        mOrderListAdapter.setOnItemClickListener(new OrderListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(OrderInfo orderInfo, int position) {
                new AlertDialog.Builder(mBaseActivity)
                        .setTitle("订单删除提醒")
                        .setMessage("确定要删除吗？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                delOrder(orderInfo.getOrder_id());
                            }
                        })

                        .show();
            }
        });
    }

    @Override
    protected void initData() {
        queryOrderByUsername();
    }

    /**
     * 删除订单
     */
    private void delOrder(int order_id) {
        OkGo.<String>get(ApiConstants.DEL_ORDER_BY_ID_URL)
                .params("order_id", order_id)
                .execute(new HttpStringCallback(null) {
                    @Override
                    protected void onSuccess(String msg, String response) {
                        showToast(msg);
                        initData();
                    }

                    @Override
                    protected void onError(String response) {

                    }
                });

    }

    /**
     * 查询用户订单
     */
    private void queryOrderByUsername() {
        OkGo.<String>get(ApiConstants.QUERY_ORDER_BY_USERNAME_URL)
                .params("username", UserInfo.getUserInfo().getUsername())
                .execute(new HttpStringCallback(null) {
                    @Override
                    protected void onSuccess(String msg, String response) {
                        OrderListInfo orderListInfo = GsonUtils.parseJson(response, OrderListInfo.class);
                        if (null != mOrderListAdapter) {
                            mOrderListAdapter.setListData(orderListInfo.getList());
                        }
                    }

                    @Override
                    protected void onError(String response) {

                    }
                });
    }

}